package com.cema.manage.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.project.manage.configuration.domain.Configuration;
import com.cema.manage.project.manage.configuration.service.IConfigurationService;
import com.cema.manage.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RedisCacheInitializer {
    @Autowired
    private RedisService redisService;

    @Autowired
    private IConfigurationService configurationService;

    @PostConstruct
    public void init(){
        List<Configuration> configurationList = configurationService.selectList(new EntityWrapper<>());
        if(ObjectUtil.isNull(configurationList)){
            return;
        }
        Map<String, String> keysAndValues = new HashMap<>();
        for (Configuration configuration: configurationList) {
            keysAndValues.put(configuration.getKey(),configuration.getValue());
        }
        redisService.setMap(keysAndValues);
    }
}
